package com.oc.mba.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.oc.mba.entity.OcPaper;
import com.oc.mba.entity.OcQuestion;
import com.oc.mba.entity.OcQuestionOption;
import com.oc.mba.entity.OcQuestionSeckill;
import com.oc.mba.entity.OcQuestionSolution;

public class CommUtil {

	/**
	 * 取得最大字符串
	 * @param len
	 * @param sourceStr
	 * @return
	 */
	public static String getMaxString(int len, String sourceStr) {
		if (sourceStr == null) {
			return null;
		}
		if (sourceStr.length() > len) {
			return sourceStr.substring(0, len);
		}
		return sourceStr;
	}

	public static Map<String, Object> toMap(OcPaper paper) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("id", paper.getId());
		resultMap.put("paperTitle", paper.getPaperTitle());
		resultMap.put("paperDesc", paper.getPaperDesc());
		resultMap.put("paperSerial", paper.getPaperSerial());
		resultMap.put("paperType", paper.getPaperType());
		resultMap.put("paperTypeTitle", paper.getPaperTypeTitle());
		resultMap.put("paperPrice", paper.getPaperPrice());
		resultMap.put("paperVersion", paper.getPaperVersion());
		resultMap.put("paperStatus", paper.getPaperStatus());
		resultMap.put("paperQuestionCount", paper.getOcPaperQuestions().size());
		return resultMap;
	}
	
	public static Map<String, Object> toMap(OcQuestion question) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("id", question.getId());
		resultMap.put("questionSerial", question.getQuestionSerial());
		resultMap.put("questionType", question.getQuestionType());
		resultMap.put("questionIndex", question.getQuestionIndex());
		resultMap.put("questionTitle", question.getQuestionTitle());
		resultMap.put("questionStatus", question.getQuestionStatus());
		resultMap.put("questionTag", question.getQuestionTag());
		resultMap.put("questionImage", question.getQuestionImage());
		resultMap.put("questionImage1", question.getQuestionImage1());
		resultMap.put("questionImage2", question.getQuestionImage2());
		
		List<Map<String, Object>> questionOptions = new ArrayList<Map<String, Object>>();
		Set<OcQuestionOption> options = question.getOcQuestionOptions();
		for (OcQuestionOption ocQuestionOption : options) {
			Map<String, Object> optionMap = new HashMap<String, Object>();
			optionMap.put("id", ocQuestionOption.getId());
			optionMap.put("optionContent", ocQuestionOption.getOptionContent());
			optionMap.put("optionCorrect", ocQuestionOption.getOptionCorrect());
			questionOptions.add(optionMap);
		}
		resultMap.put("questionOptions", questionOptions);
		
		List<Map<String, Object>> questionSolutions = new ArrayList<Map<String, Object>>();
		Set<OcQuestionSolution> solutions = question.getOcQuestionSolutions();
		for (OcQuestionSolution solution : solutions) {
			Map<String, Object> solutionMap = new HashMap<String, Object>();
			solutionMap.put("id", solution.getId());
			solutionMap.put("solutionContent", solution.getSolutionContent());
			questionSolutions.add(solutionMap);
		}
		resultMap.put("questionSolutions", questionSolutions);
		
		List<Map<String, Object>> questionSeckills = new ArrayList<Map<String, Object>>();
		Set<OcQuestionSeckill> seckills = question.getOcQuestionSeckills();
		for (OcQuestionSeckill seckill : seckills) {
			Map<String, Object> seckillMap = new HashMap<String, Object>();
			seckillMap.put("id", seckill.getId());
			seckillMap.put("seckillContent", seckill.getSeckillContent());
			questionSeckills.add(seckillMap);
		}
		resultMap.put("questionSeckills", questionSeckills);
		return resultMap;
	}
	
}
